package com.hzy.fivepalace.wx.controller.api;

import com.hzy.fivepalace.common.util.TableDataInfo;
import com.hzy.fivepalace.db.domain.DtsOrder;
import com.hzy.fivepalace.db.service.PayService;
import com.hzy.fivepalace.db.vo.wx.WxApplyAfterSaleVo;
import com.hzy.fivepalace.db.vo.wx.WxUploadReturnLogisticsVo;
import com.hzy.fivepalace.wx.annotation.LoginUser;
import com.hzy.fivepalace.wx.controller.base.BaseController;
import com.hzy.fivepalace.wx.service.WxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/wx/order")
@Validated
@Api(tags = "订单服务")
public class WxOrderController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(WxOrderController.class);

    @Autowired
    private WxOrderService wxOrderService;



    /**
     * 各订单状态数量
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserOrderCount")
    @ApiOperation(value = "各订单状态数量")
    public Object getOrderCount(@LoginUser(login = false) @ApiParam(hidden = true) Integer userId) {


        return wxOrderService.getUserOrderCount(userId);
    }

    /**
     * 订单列表
     *
     * @param userId
     * @param status
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/orderList")
    @ApiOperation(value = "订单列表")
    public TableDataInfo orderList(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "状态:为空就查所有,新增-1状态(查售后订单列表)") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer size) {

        return wxOrderService.getUserOrderList(page, size, userId, status);
    }

    /**
     * 订单详情
     *
     * @param userId
     * @param orderId
     * @return
     */
    @GetMapping("/orderDetails")
    @ApiOperation(value = "订单详情")
    public Object orderDetails(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "订单id", defaultValue = "") @NotNull Integer orderId) {
        return wxOrderService.orderDetails(userId, orderId);
    }


    /**
     * 删除订单
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("/orderDel")
    @ApiOperation(value = "删除订单")
    public Object orderDel(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{orderId:1}") @RequestBody String body) {

        return wxOrderService.orderDel(body, userId);
    }

    /**
     * 支付订单
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("/orderPay")
    @ApiOperation(value = "支付订单")
    public Object orderPay(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{orderId:1}") @RequestBody String body) {

        return wxOrderService.orderPay(body, userId);
    }

    /**
     * 取消订单
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("/orderCancel")
    @ApiOperation(value = "取消订单")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object orderCancel(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{orderId:1}") @RequestBody String body) {

        return wxOrderService.orderCancel(body, userId);
    }

    /**
     * 订单退款
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("/orderRefund")
    @ApiOperation(value = "订单退款")
    public Object orderRefund(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{orderId:1}") @RequestBody String body) {

        return wxOrderService.orderRefund(body, userId);
    }

    @PostMapping("/cancelAfterSale")
    @ApiOperation(value = "取消售后申请")
    public Object cancelAfterSale(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{orderId:1}") @RequestBody String body) {

        return wxOrderService.cancelAfterSale(body, userId);
    }

    /**
     * 申请售后
     *
     * @param userId
     * @param vo
     * @return
     */
    @PostMapping("/applyAfterSale")
    @ApiOperation(value = "申请售后")
    public Object applyAfterSale(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @RequestBody WxApplyAfterSaleVo vo) {

        return wxOrderService.applyAfterSale(vo, userId);
    }

    /**
     * 上传退货物流
     *
     * @param userId
     * @param vo
     * @return
     */
    @PostMapping("/uploadReturnLogistics")
    @ApiOperation(value = "上传退货物流")
    public Object uploadReturnLogistics(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @RequestBody WxUploadReturnLogisticsVo vo) {

        return wxOrderService.uploadReturnLogistics(vo, userId);
    }

    /**
     * 查看退货物流
     *
     * @param userId
     * @param orderId
     * @return
     */
    @GetMapping("/queryReturnLogistics")
    @ApiOperation(value = "查看退货物流")
    public Object queryReturnLogistics(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "订单id") @NotNull Integer orderId
    ) {

        return wxOrderService.queryReturnLogistics(userId, orderId);
    }

    /**
     * 获取所有快递公司
     *
     * @param userId
     * @return
     */
    @GetMapping("/getAllShipChannelCode")
    @ApiOperation(value = "获取所有快递公司编码")
    public Object getAllShipChannelCode(
            @LoginUser @ApiParam(hidden = true) Integer userId
    ) {

        return wxOrderService.getAllShipChannelCode();
    }

    /**
     * 确认收货
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("/confirm")
    @ApiOperation(value = "确认收货")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object confirm(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{orderId:1,takeImg:url1}") @RequestBody String body
    ) {

        return wxOrderService.confirm(body, userId);
    }

    /**
     * 评价
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("/comment")
    @ApiOperation(value = "评价")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object comment(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{orderId:1,comment:[{'goodsId':'商品id','content':'评价内容','star':'评分','img':['图片地址1','图片地址2']}]}") @RequestBody String body
    ) {
        return wxOrderService.commentOrder(body, userId);
    }

    /**
     * 获取退货地址,订单状态为11(待上传物流信息)用
     *
     * @param userId
     * @return
     */
    @GetMapping("/getReturnAdress")
    @ApiOperation(value = "获取退货地址,订单状态为11用")
    public Object getReturnAdress(@LoginUser @ApiParam(hidden = true) Integer userId) {
        return wxOrderService.getReturnAdress();
    }

    /**
     * 查看物流
     *
     * @param userId
     * @param orderId
     * @return
     */
    @GetMapping("/queryLogistics")
    @ApiOperation(value = "查看订单物流")
    public Object queryLogistics(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "订单id") @NotNull Integer orderId) {
        return wxOrderService.queryLogistics(userId, orderId);
    }

    /**
     * 我的评价列表
     *
     * @param userId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/myCommentsList")
    @ApiOperation(value = "我的评价列表")
    public TableDataInfo myCommentsList(@LoginUser @ApiParam(hidden = true) Integer userId,
                                        @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                                        @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer size) {
        return wxOrderService.getUserCommentList(userId, page, size);
    }

    /**
     * 删除评价
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("/delComments")
    @ApiOperation(value = "删除评价")
    public Object delComments(
            @LoginUser @ApiParam(hidden = true) Integer userId,
            @ApiParam(value = "{commentId:1}") @RequestBody String body) {
        return wxOrderService.delComments(body, userId);
    }
}
